using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Xml;
using Common;

namespace WizLighter
{
    public partial class MainForm : Form
    {
        #region getting ready

        LEDWiz ledwiz = null;
        public MainForm()
        {

            InitializeComponent();

        }

        public Bitmap b;

        public Graphics g;

        private int swidth, sheight, c = 0, CyclesInt = 0;


        public class DWM
        {
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmEnableComposition(bool bEnable);
        }
        #endregion

        #region Load


        private void MainForm_Load(object sender, EventArgs e)
        {
            if (OperatingSystemVersion.Current > OSVersionInfo.WinXP)
            {
                ControlAero(false);
                EnumScreens();
                ledwiz = new LEDWiz(IntPtr.Zero);
                ledwiz.StartupLighting();
                Wizer();
                GETsettingS();
                this.TopMost = false;

            }
            else
            {
                EnumScreens();
                ledwiz = new LEDWiz(IntPtr.Zero);
                ledwiz.StartupLighting();
                Wizer();
                GETsettingS();
                this.TopMost = false;
            }
        }

        #endregion

        #region Set Aero fuction
        // call to this is under mainforum load
        public readonly uint DWM_EC_DISABLECOMPOSITION = 0;
        public readonly uint DWM_EC_ENABLECOMPOSITION = 1;
        [DllImport("dwmapi.dll", EntryPoint = "DwmEnableComposition")]
        protected extern static uint Win32DwmEnableComposition(uint uCompositionAction);

        public bool ControlAero(bool enable)
        {
            try
            {
                if (enable)
                    Win32DwmEnableComposition(DWM_EC_ENABLECOMPOSITION);
                if (!enable)
                    Win32DwmEnableComposition(DWM_EC_DISABLECOMPOSITION);

                return true;
            }
            catch { return false; }
        }

        #endregion

        #region Capture Loop

        private void CaptureTimer_Tick(object sender, EventArgs e)
        {
            if (GlobalVariables.mode == 0)
            {
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                DeskCapture();
                int Red = 0, Green = 0, Blue = 0, Average = 0;
                for (int I = 0; I < 25; I++) //Copy the current color into the send byte array
                {
                    Red = (GlobalVariables.CurrentColor[I] + GlobalVariables.PreviousColor[I] + GlobalVariables.PreviousColor2[I]) / 3;
                    Green = (GlobalVariables.CurrentColor[I + 3] + GlobalVariables.PreviousColor[I + 3] + GlobalVariables.PreviousColor2[I + 3]) / 3;
                    Blue = (GlobalVariables.CurrentColor[I + 6] + GlobalVariables.PreviousColor[I + 6] + GlobalVariables.PreviousColor2[I + 6]) / 3;
                    //Red = GlobalVariables.PreviousColor[I];
                    //Green = GlobalVariables.PreviousColor[I + 3];
                    //Blue = GlobalVariables.PreviousColor[I + 6];

                    Average = (Red + Green + Blue) / 3;

                    //Increase of color by extrapolation, or interpolation, I don't know :P
                    Red = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Red) / 10);
                    Green = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Green) / 10);
                    Blue = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Blue) / 10);
                    
                    if (Red > 255) Red = 255;
                    if (Green > 255) Green = 255;
                    if (Blue > 255) Blue = 255;
                    if (Red < 0) Red = 0;
                    if (Green < 0) Green = 0;
                    if (Blue < 0) Blue = 0;

                    GlobalVariables.SendByte[I] = (byte)Red;
                    GlobalVariables.SendByte[I + 3] = (byte)Green;
                    GlobalVariables.SendByte[I + 6] = (byte)Blue;

                }
            }
            else if (GlobalVariables.mode == 1)
            {
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                DeskCapture();
                GlobalVariables.Counter++;
                if (GlobalVariables.Counter == 10)
                {
                    GlobalVariables.Counter = 0;
                    DeskCapture();
                    int Red = 0, Green = 0, Blue = 0, Average = 0;
                    for (int I = 0; I < 25; I++) //Copy the current color into the send byte array
                    {
                        Red = GlobalVariables.CurrentColor[I];
                        Green = GlobalVariables.CurrentColor[I + 3];
                        Blue = GlobalVariables.CurrentColor[I + 6];

                        Average = (Red + Green + Blue) / 3;

                        Red = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Red) / 10);
                        Green = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Green) / 10);
                        Blue = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Blue) / 10);

                        if (Red > 255) Red = 255;
                        if (Green > 255) Green = 255;
                        if (Blue > 255) Blue = 255;
                        if (Red < 0) Red = 0;
                        if (Green < 0) Green = 0;
                        if (Blue < 0) Blue = 0;

                        GlobalVariables.SendByte[I] = (byte)Red;
                        GlobalVariables.SendByte[I + 3] = (byte)Green;
                        GlobalVariables.SendByte[I + 6] = (byte)Blue;

                    }

                }
                else
                {
                    DeskCapture();
                    int Red = 0, Green = 0, Blue = 0, Average = 0;
                    for (int I = 0; I < 25; I++) //Copy the current color into the send byte array
                    {
                        Red = GlobalVariables.PreviousColor[I] + ((GlobalVariables.CurrentColor[I] - GlobalVariables.PreviousColor[I]) * GlobalVariables.Counter / 10);
                        Green = GlobalVariables.PreviousColor[I + 3] + ((GlobalVariables.CurrentColor[I + 3] - GlobalVariables.PreviousColor[I + 3]) * GlobalVariables.Counter / 10);
                        Blue = GlobalVariables.PreviousColor[I + 6] + ((GlobalVariables.CurrentColor[I + 6] - GlobalVariables.PreviousColor[I + 6]) * GlobalVariables.Counter / 10);


                        
                        Average = (Red + Green + Blue) / 3;

                        Red = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Red) / 10);
                        Green = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Green) / 10);
                        Blue = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Blue) / 10);

                        if (Red > 255) Red = 255;
                        if (Green > 255) Green = 255;
                        if (Blue > 255) Blue = 255;
                        if (Red < 0) Red = 0;
                        if (Green < 0) Green = 0;
                        if (Blue < 0) Blue = 0;

                        GlobalVariables.SendByte[I] = (byte)Red;
                        GlobalVariables.SendByte[I + 3] = (byte)Green;
                        GlobalVariables.SendByte[I + 6] = (byte)Blue;

                    }
                
                
                }

            }
            else if (GlobalVariables.mode == 2)
            {
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
//                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;
                DeskCapture();
                int Red = 0, Green = 0, Blue = 0, Average = 0;
                for (int I = 0; I < 25; I++) //Copy the current color into the send byte array
                {
                    Red = (GlobalVariables.CurrentColor[I] + GlobalVariables.PreviousColor[I] + GlobalVariables.PreviousColor2[I] + GlobalVariables.PreviousColor3[I] + GlobalVariables.PreviousColor4[I] + GlobalVariables.PreviousColor5[I] + GlobalVariables.PreviousColor6[I] + GlobalVariables.PreviousColor7[I] + GlobalVariables.PreviousColor8[I] + GlobalVariables.PreviousColor9[I]) / 10;
                    Green = (GlobalVariables.CurrentColor[I + 3] + GlobalVariables.PreviousColor[I + 3] + GlobalVariables.PreviousColor2[I + 3] + GlobalVariables.PreviousColor3[I + 3] + GlobalVariables.PreviousColor4[I + 3] + GlobalVariables.PreviousColor5[I + 3] + GlobalVariables.PreviousColor6[I + 3] + GlobalVariables.PreviousColor7[I + 3] + GlobalVariables.PreviousColor8[I + 3] + GlobalVariables.PreviousColor9[I + 3]) / 10;
                    Blue = (GlobalVariables.CurrentColor[I + 6] + GlobalVariables.PreviousColor[I + 6] + GlobalVariables.PreviousColor2[I + 6] + GlobalVariables.PreviousColor3[I + 6] + GlobalVariables.PreviousColor4[I + 6] + GlobalVariables.PreviousColor5[I + 6] + GlobalVariables.PreviousColor6[I + 6] + GlobalVariables.PreviousColor7[I + 6] + GlobalVariables.PreviousColor8[I + 6] + GlobalVariables.PreviousColor9[I + 6]) / 10;

                    Average = (Red + Green + Blue) / 3;

                    //Increase of color by extrapolation, or interpolation, I don't know :P
                    Red = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Red) / 10);
                    Green = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Green) / 10);
                    Blue = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Blue) / 10);

                    if (Red > 255) Red = 255;
                    if (Green > 255) Green = 255;
                    if (Blue > 255) Blue = 255;
                    if (Red < 0) Red = 0;
                    if (Green < 0) Green = 0;
                    if (Blue < 0) Blue = 0;

                    GlobalVariables.SendByte[I] = (byte)Red;
                    GlobalVariables.SendByte[I + 3] = (byte)Green;
                    GlobalVariables.SendByte[I + 6] = (byte)Blue;

                }
            }
            else if (GlobalVariables.mode == 3)
            {
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                //                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;
                DeskCapture();
                int Red = 0, Green = 0, Blue = 0, Average = 0;
                for (int I = 0; I < 25; I++) //Copy the current color into the send byte array
                {
                    Red = (GlobalVariables.CurrentColor[I] + GlobalVariables.PreviousColor[I] + GlobalVariables.PreviousColor2[I] + GlobalVariables.PreviousColor3[I] + GlobalVariables.PreviousColor4[I] + GlobalVariables.PreviousColor5[I] + GlobalVariables.PreviousColor6[I] + GlobalVariables.PreviousColor7[I] + GlobalVariables.PreviousColor8[I] + GlobalVariables.PreviousColor9[I]
                        + GlobalVariables.PreviousColor10[I] + GlobalVariables.PreviousColor11[I] + GlobalVariables.PreviousColor12[I] + GlobalVariables.PreviousColor13[I] + GlobalVariables.PreviousColor14[I] + GlobalVariables.PreviousColor15[I] + GlobalVariables.PreviousColor16[I] + GlobalVariables.PreviousColor17[I] + GlobalVariables.PreviousColor18[I] + GlobalVariables.PreviousColor19[I]
                        + GlobalVariables.PreviousColor20[I] + GlobalVariables.PreviousColor21[I] + GlobalVariables.PreviousColor22[I] + GlobalVariables.PreviousColor23[I] + GlobalVariables.PreviousColor24[I] + GlobalVariables.PreviousColor25[I] + GlobalVariables.PreviousColor26[I] + GlobalVariables.PreviousColor27[I] + GlobalVariables.PreviousColor28[I] + GlobalVariables.PreviousColor29[I]
                        + GlobalVariables.PreviousColor30[I]) / 30;

                    Green = (GlobalVariables.CurrentColor[I + 3] + GlobalVariables.PreviousColor[I + 3] + GlobalVariables.PreviousColor2[I + 3] + GlobalVariables.PreviousColor3[I + 3] + GlobalVariables.PreviousColor4[I + 3] + GlobalVariables.PreviousColor5[I + 3] + GlobalVariables.PreviousColor6[I + 3] + GlobalVariables.PreviousColor7[I + 3] + GlobalVariables.PreviousColor8[I + 3] + GlobalVariables.PreviousColor9[I + 3]
                          + GlobalVariables.PreviousColor10[I + 3] + GlobalVariables.PreviousColor11[I + 3] + GlobalVariables.PreviousColor12[I + 3] + GlobalVariables.PreviousColor13[I + 3] + GlobalVariables.PreviousColor14[I + 3] + GlobalVariables.PreviousColor15[I + 3] + GlobalVariables.PreviousColor16[I + 3] + GlobalVariables.PreviousColor17[I + 3] + GlobalVariables.PreviousColor18[I + 3] + GlobalVariables.PreviousColor19[I + 3]
                          + GlobalVariables.PreviousColor20[I + 3] + GlobalVariables.PreviousColor21[I + 3] + GlobalVariables.PreviousColor22[I + 3] + GlobalVariables.PreviousColor23[I + 3] + GlobalVariables.PreviousColor24[I + 3] + GlobalVariables.PreviousColor25[I + 3] + GlobalVariables.PreviousColor26[I + 3] + GlobalVariables.PreviousColor27[I + 3] + GlobalVariables.PreviousColor28[I + 3] + GlobalVariables.PreviousColor29[I + 3]
                          + GlobalVariables.PreviousColor30[I + 3]) / 30;

                    Blue = (GlobalVariables.CurrentColor[I + 6] + GlobalVariables.PreviousColor[I + 6] + GlobalVariables.PreviousColor2[I + 6] + GlobalVariables.PreviousColor3[I + 6] + GlobalVariables.PreviousColor4[I + 6] + GlobalVariables.PreviousColor5[I + 6] + GlobalVariables.PreviousColor6[I + 6] + GlobalVariables.PreviousColor7[I + 6] + GlobalVariables.PreviousColor8[I + 6] + GlobalVariables.PreviousColor9[I + 6]
                         + GlobalVariables.PreviousColor10[I + 6] + GlobalVariables.PreviousColor11[I + 6] + GlobalVariables.PreviousColor12[I + 6] + GlobalVariables.PreviousColor13[I + 6] + GlobalVariables.PreviousColor14[I + 6] + GlobalVariables.PreviousColor15[I + 6] + GlobalVariables.PreviousColor16[I + 6] + GlobalVariables.PreviousColor17[I + 6] + GlobalVariables.PreviousColor18[I + 6] + GlobalVariables.PreviousColor19[I + 6]
                         + GlobalVariables.PreviousColor20[I + 6] + GlobalVariables.PreviousColor21[I + 6] + GlobalVariables.PreviousColor22[I + 6] + GlobalVariables.PreviousColor23[I + 6] + GlobalVariables.PreviousColor24[I + 6] + GlobalVariables.PreviousColor25[I + 6] + GlobalVariables.PreviousColor26[I + 6] + GlobalVariables.PreviousColor27[I + 6] + GlobalVariables.PreviousColor28[I + 6] + GlobalVariables.PreviousColor29[I + 6]
                         + GlobalVariables.PreviousColor30[I + 6]) / 30;


                    Average = (Red + Green + Blue) / 3;

                    //Increase of color by extrapolation, or interpolation, I don't know :P
                    Red = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Red) / 10);
                    Green = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Green) / 10);
                    Blue = (10 - (int)Colorslider.Value) * Average / 10 + (((int)Colorslider.Value * Blue) / 10);

                    if (Red > 255) Red = 255;
                    if (Green > 255) Green = 255;
                    if (Blue > 255) Blue = 255;
                    if (Red < 0) Red = 0;
                    if (Green < 0) Green = 0;
                    if (Blue < 0) Blue = 0;

                    GlobalVariables.SendByte[I] = (byte)Red;
                    GlobalVariables.SendByte[I + 3] = (byte)Green;
                    GlobalVariables.SendByte[I + 6] = (byte)Blue;

                }
            }
            else if (GlobalVariables.mode == 4)
            {
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                DeskCapture();
                int Red = 0, Green = 0, Blue = 0, Average = 0;
                int ColorInc = 0, rdiff, gdiff, bdiff, currentaverage, previousaverage, rdiff2, gdiff2, bdiff2;
                bool noagressive;
                GlobalVariables.ColorIncTemp = GlobalVariables.ColorIncTemp + 1;
                for (int I = 0; I < 25; I++) //Copy the current color into the send byte array
                {
                    GlobalVariables.RealByte[I] = GlobalVariables.CurrentColor[I];
                    GlobalVariables.RealByte[I + 3] = GlobalVariables.CurrentColor[I + 3];
                    GlobalVariables.RealByte[I + 6] = GlobalVariables.CurrentColor[I + 6];

                    rdiff = GlobalVariables.CurrentColor[I] - GlobalVariables.PreviousColor[I];
                    gdiff = GlobalVariables.CurrentColor[I + 3] - GlobalVariables.PreviousColor[I + 3];
                    bdiff = GlobalVariables.CurrentColor[I + 6] - GlobalVariables.PreviousColor[I + 6];

                    currentaverage = (GlobalVariables.CurrentColor[I] + GlobalVariables.CurrentColor[I + 3] + GlobalVariables.CurrentColor[I + 6]) / 3;
                    previousaverage = (GlobalVariables.PreviousColor[I] + GlobalVariables.PreviousColor[I + 3] + GlobalVariables.PreviousColor[I + 6]) / 3;

                    noagressive = false;

                    if ( (currentaverage < AggressiveLevel.Value) && (SmoothDarkness.Checked == true) )
                    {
                        noagressive = true;
                    }

                    if ( (rdiff > fsr.Value || gdiff > fsr.Value || bdiff > fsr.Value) && (currentaverage > previousaverage) && (noagressive == false) )
                    {
                        Red = GlobalVariables.CurrentColor[I];
                        Green = GlobalVariables.CurrentColor[I + 3];
                        Blue = GlobalVariables.CurrentColor[I + 6];
                        Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor, 9);
                        Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor2, 9);
                        Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor3, 9);
                        Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor4, 9);
                        Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor5, 9);
                        Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor6, 9);
                        Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor7, 9);
                        Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor8, 9);
                        Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor9, 9);
                        c = 0;
                        GlobalVariables.ColorIncP = 0;
                        GlobalVariables.ColorIncTemp = 0;
                    }

                    else if (((rdiff + gdiff + bdiff) / 3 > fsr.Value / 3) || (c < 3 && c > 0))
                    {
                        Red = (GlobalVariables.CurrentColor[I] + GlobalVariables.PreviousColor[I] + GlobalVariables.PreviousColor2[I]) / 3;
                        Green = (GlobalVariables.CurrentColor[I + 3] + GlobalVariables.PreviousColor[I + 3] + GlobalVariables.PreviousColor2[I + 3]) / 3;
                        Blue = (GlobalVariables.CurrentColor[I + 6] + GlobalVariables.PreviousColor[I + 6] + GlobalVariables.PreviousColor2[I + 6]) / 3;
                        c=c+1;
                        if (c == 3)
                        {
                            c = 0;
                        }
                    }
                    else
                    {
                        Red = (GlobalVariables.CurrentColor[I] + GlobalVariables.PreviousColor[I] + GlobalVariables.PreviousColor2[I] + GlobalVariables.PreviousColor3[I] + GlobalVariables.PreviousColor4[I] + GlobalVariables.PreviousColor5[I] + GlobalVariables.PreviousColor6[I] + GlobalVariables.PreviousColor7[I] + GlobalVariables.PreviousColor8[I] + GlobalVariables.PreviousColor9[I]) / 10;
                        Green = (GlobalVariables.CurrentColor[I + 3] + GlobalVariables.PreviousColor[I + 3] + GlobalVariables.PreviousColor2[I + 3] + GlobalVariables.PreviousColor3[I + 3] + GlobalVariables.PreviousColor4[I + 3] + GlobalVariables.PreviousColor5[I + 3] + GlobalVariables.PreviousColor6[I + 3] + GlobalVariables.PreviousColor7[I + 3] + GlobalVariables.PreviousColor8[I + 3] + GlobalVariables.PreviousColor9[I + 3]) / 10;
                        Blue = (GlobalVariables.CurrentColor[I + 6] + GlobalVariables.PreviousColor[I + 6] + GlobalVariables.PreviousColor2[I + 6] + GlobalVariables.PreviousColor3[I + 6] + GlobalVariables.PreviousColor4[I + 6] + GlobalVariables.PreviousColor5[I + 6] + GlobalVariables.PreviousColor6[I + 6] + GlobalVariables.PreviousColor7[I + 6] + GlobalVariables.PreviousColor8[I + 6] + GlobalVariables.PreviousColor9[I + 6]) / 10;
                    }

                    Average = (Red + Green + Blue) / 3;

                    
                    
                    // Color Increasing Smoother
                    ColorInc = 10 + GlobalVariables.ColorIncP;

                    if ((int)Colorslider.Value > 10)
                    {
                        if (GlobalVariables.CurrentColor[I] > GlobalVariables.PreviousColor[I])
                        {
                            rdiff2 = GlobalVariables.CurrentColor[I] - GlobalVariables.PreviousColor[I];
                        }
                        else
                        {
                            rdiff2 = GlobalVariables.PreviousColor[I] - GlobalVariables.CurrentColor[I];
                        }

                        if (GlobalVariables.CurrentColor[I + 3] > GlobalVariables.PreviousColor[I + 3])
                        {
                            gdiff2 = GlobalVariables.CurrentColor[I + 3] - GlobalVariables.PreviousColor[I + 3];
                        }
                        else
                        {
                            gdiff2 = GlobalVariables.PreviousColor[I + 3] - GlobalVariables.CurrentColor[I + 3];
                        }

                        if (GlobalVariables.CurrentColor[I + 6] > GlobalVariables.PreviousColor[I + 6])
                        {
                            bdiff2 = GlobalVariables.CurrentColor[I + 6] - GlobalVariables.PreviousColor[I + 6];
                        }
                        else
                        {
                            bdiff2 = GlobalVariables.PreviousColor[I + 6] - GlobalVariables.CurrentColor[I + 6];
                        }

//                        label40.Text = ColorInc.ToString();

                        if ((rdiff2 < 10 || gdiff2 < 10 || bdiff2 < 10) && (ColorInc < (int)Colorslider.Value))
                        {
                            if (GlobalVariables.ColorIncTemp < (int)ColorIncSmooth.Value)
                            {
                                GlobalVariables.ColorIncTemp = GlobalVariables.ColorIncTemp + 1;
                            }
                            else
                            {
                                GlobalVariables.ColorIncP = GlobalVariables.ColorIncP + 1;
                                GlobalVariables.ColorIncTemp = 0;
                            }
                        }
                        if ((rdiff2 > 10 || gdiff2 > 10 || bdiff2 > 10) && (ColorInc > 10))
                        {
                            GlobalVariables.ColorIncP = GlobalVariables.ColorIncP - 1;
                        }
                    }

//                    label39.Text = ColorInc.ToString();
                    if (this.WindowState != FormWindowState.Minimized)
                    {
                        numericUpDown2.Value = ColorInc;
                    }

                    //Increase of color by extrapolation, or interpolation, I don't know :P
                    Red = (10 - ColorInc) * Average / 10 + ((ColorInc * Red) / 10);
                    Green = (10 - ColorInc) * Average / 10 + ((ColorInc * Green) / 10);
                    Blue = (10 - ColorInc) * Average / 10 + ((ColorInc * Blue) / 10);

//                    label42.Text = Red.ToString();
//                    label43.Text = Green.ToString();
//                    label44.Text = Blue.ToString();

                    if (Red > 255) Red = 255;
                    if (Green > 255) Green = 255;
                    if (Blue > 255) Blue = 255;
                    if (Red < 0) Red = 0;
                    if (Green < 0) Green = 0;
                    if (Blue < 0) Blue = 0;

                    /*
                    // BACKLIGHT!!!!!
                    // make min value
                    if ((int)ColorIncSmooth.Value > 0)
                    {
                        if (Red > 0)
                        {
                            Red = ((255 - (int)ColorIncSmooth.Value) / (255 / Red) + (int)ColorIncSmooth.Value);
                        }
                        else
                        {
                            Red = (int)ColorIncSmooth.Value;
                        }
                        if (Green > 0)
                        {
                            Green = ((255 - (int)ColorIncSmooth.Value) / (255 / Green) + (int)ColorIncSmooth.Value);
                        }
                        else
                        {
                            Green = (int)ColorIncSmooth.Value;
                        }
                        if (Blue > 0)
                        {
                            Blue = ((255 - (int)ColorIncSmooth.Value) / (255 / Blue) + (int)ColorIncSmooth.Value);
                        }
                        else
                        {
                            Blue = (int)ColorIncSmooth.Value;
                        }
                    }
                     */
 

                    GlobalVariables.SendByte[I] = (byte)Red;
                    GlobalVariables.SendByte[I + 3] = (byte)Green;
                    GlobalVariables.SendByte[I + 6] = (byte)Blue;

                }
            }

            //This was added later, so it looks ugly :P


            bool send = true;
            /*
            bool send = false;

            for (int I = 0; I < 9; I++)
            {
                if (GlobalVariables.SendByte[I] != GlobalVariables.OldSendByte[I])
                {
                    send = true;
                }
            }
            */

            //if (true)
            if (send)
            {


                if (this.WindowState != FormWindowState.Minimized) //Show the average colors on screen
                {
                    ColorBB.BackColor = Color.FromArgb((int)GlobalVariables.SendByte[0], (int)GlobalVariables.SendByte[3], (int)GlobalVariables.SendByte[6]);
                    ColorAA.BackColor = Color.FromArgb((int)GlobalVariables.SendByte[1], (int)GlobalVariables.SendByte[4], (int)GlobalVariables.SendByte[7]);
                    ColorCC.BackColor = Color.FromArgb((int)GlobalVariables.SendByte[2], (int)GlobalVariables.SendByte[5], (int)GlobalVariables.SendByte[8]);
                    ColorDD.BackColor = Color.FromArgb((int)GlobalVariables.SendByte[9], (int)GlobalVariables.SendByte[12], (int)GlobalVariables.SendByte[15]);
                    ColorEE.BackColor = Color.FromArgb((int)GlobalVariables.SendByte[10], (int)GlobalVariables.SendByte[13], (int)GlobalVariables.SendByte[16]);
                    ColorFF.BackColor = Color.FromArgb((int)GlobalVariables.SendByte[11], (int)GlobalVariables.SendByte[14], (int)GlobalVariables.SendByte[17]);
                    ColorGG.BackColor = Color.FromArgb((int)GlobalVariables.SendByte[18], (int)GlobalVariables.SendByte[21], (int)GlobalVariables.SendByte[24]);
                    ColorHH.BackColor = Color.FromArgb((int)GlobalVariables.SendByte[19], (int)GlobalVariables.SendByte[22], (int)GlobalVariables.SendByte[25]);

                }
                GlobalVariables.SendByte[0] = (byte)(int)GlobalVariables.SendByte[0];
                GlobalVariables.SendByte[1] = (byte)(int)GlobalVariables.SendByte[1];
                GlobalVariables.SendByte[2] = (byte)(int)GlobalVariables.SendByte[2];
                GlobalVariables.SendByte[3] = (byte)(int)GlobalVariables.SendByte[3];
                GlobalVariables.SendByte[4] = (byte)(int)GlobalVariables.SendByte[4];
                GlobalVariables.SendByte[5] = (byte)(int)GlobalVariables.SendByte[5];
                GlobalVariables.SendByte[6] = (byte)(int)GlobalVariables.SendByte[6];
                GlobalVariables.SendByte[7] = (byte)(int)GlobalVariables.SendByte[7];
                GlobalVariables.SendByte[8] = (byte)(int)GlobalVariables.SendByte[8];
                GlobalVariables.SendByte[9] = (byte)(int)GlobalVariables.SendByte[9];
                GlobalVariables.SendByte[10] = (byte)(int)GlobalVariables.SendByte[10];
                GlobalVariables.SendByte[11] = (byte)(int)GlobalVariables.SendByte[11];
                GlobalVariables.SendByte[12] = (byte)(int)GlobalVariables.SendByte[12];
                GlobalVariables.SendByte[13] = (byte)(int)GlobalVariables.SendByte[13];
                GlobalVariables.SendByte[14] = (byte)(int)GlobalVariables.SendByte[14];
                GlobalVariables.SendByte[15] = (byte)(int)GlobalVariables.SendByte[15];
                GlobalVariables.SendByte[16] = (byte)(int)GlobalVariables.SendByte[16];
                GlobalVariables.SendByte[17] = (byte)(int)GlobalVariables.SendByte[17];
                GlobalVariables.SendByte[18] = (byte)(int)GlobalVariables.SendByte[18];
                GlobalVariables.SendByte[19] = (byte)(int)GlobalVariables.SendByte[19];
                GlobalVariables.SendByte[20] = (byte)(int)GlobalVariables.SendByte[20];
                GlobalVariables.SendByte[21] = (byte)(int)GlobalVariables.SendByte[21];
                GlobalVariables.SendByte[22] = (byte)(int)GlobalVariables.SendByte[22];
                GlobalVariables.SendByte[23] = (byte)(int)GlobalVariables.SendByte[23];
                GlobalVariables.SendByte[24] = (byte)(int)GlobalVariables.SendByte[24];
                GlobalVariables.SendByte[25] = (byte)(int)GlobalVariables.SendByte[25];
                GlobalVariables.SendByte[26] = (byte)(int)GlobalVariables.SendByte[26];
                GlobalVariables.SendByte[27] = (byte)(int)GlobalVariables.SendByte[27];
                Send(); //send the values
                //this.Text = "sending";
            }
            else
            {
                //this.Text = "not sending";
            }
            
            Array.Copy(GlobalVariables.SendByte, GlobalVariables.OldSendByte, 27);
            
        }

        #endregion

        #region Closing

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.TopMost = false;
            SAVEsettingS();
            ledwiz.ShutdownLighting();
            ledwiz.Dispose();
            Array.Clear(GlobalVariables.SendByte, 0, 96);
        }
       
        #endregion

        #region SENDING out colors

        private void Send() //send out the colors
        {

            int AAR = (int)((double)GlobalVariables.SendByte[0] / (double)5.3125);
            int BBR = (int)((double)GlobalVariables.SendByte[1] / (double)5.3125);
            int CCR = (int)((double)GlobalVariables.SendByte[2] / (double)5.3125);
            int AAG = (int)((double)GlobalVariables.SendByte[3] / (double)5.3125);
            int BBG = (int)((double)GlobalVariables.SendByte[4] / (double)5.3125);
            int CCG = (int)((double)GlobalVariables.SendByte[5] / (double)5.3125);
            int AAB = (int)((double)GlobalVariables.SendByte[6] / (double)5.3125);
            int BBB = (int)((double)GlobalVariables.SendByte[7] / (double)5.3125);
            int CCB = (int)((double)GlobalVariables.SendByte[8] / (double)5.3125);
            int DDR = (int)((double)GlobalVariables.SendByte[9] / (double)5.3125);
            int EER = (int)((double)GlobalVariables.SendByte[10] / (double)5.3125);
            int FFR = (int)((double)GlobalVariables.SendByte[11] / (double)5.3125);
            int DDG = (int)((double)GlobalVariables.SendByte[12] / (double)5.3125);
            int EEG = (int)((double)GlobalVariables.SendByte[13] / (double)5.3125);
            int FFG = (int)((double)GlobalVariables.SendByte[14] / (double)5.3125);
            int DDB = (int)((double)GlobalVariables.SendByte[15] / (double)5.3125);
            int EEB = (int)((double)GlobalVariables.SendByte[16] / (double)5.3125);
            int FFB = (int)((double)GlobalVariables.SendByte[17] / (double)5.3125);
            int GGR = (int)((double)GlobalVariables.SendByte[18] / (double)5.3125);
            int HHR = (int)((double)GlobalVariables.SendByte[19] / (double)5.3125);
      //    int IIR = (int)((double)GlobalVariables.SendByte[20] / (double)5.3125);
            int GGG = (int)((double)GlobalVariables.SendByte[21] / (double)5.3125);
            int HHG = (int)((double)GlobalVariables.SendByte[22] / (double)5.3125);
      //    int IIG = (int)((double)GlobalVariables.SendByte[23] / (double)5.3125);
            int GGB = (int)((double)GlobalVariables.SendByte[24] / (double)5.3125);
            int HHB = (int)((double)GlobalVariables.SendByte[25] / (double)5.3125);
      //    int IIB = (int)((double)GlobalVariables.SendByte[26] / (double)5.3125);


            string cmd = txtColors.Text;
            cmd = cmd.Replace("AR", AAR.ToString());
            cmd = cmd.Replace("AG", AAG.ToString());
            cmd = cmd.Replace("AB", AAB.ToString());
            cmd = cmd.Replace("BR", BBR.ToString());
            cmd = cmd.Replace("BG", BBG.ToString());
            cmd = cmd.Replace("BB", BBB.ToString());
            cmd = cmd.Replace("CR", CCR.ToString());
            cmd = cmd.Replace("CG", CCG.ToString());
            cmd = cmd.Replace("CB", CCB.ToString());
            cmd = cmd.Replace("DR", DDR.ToString());
            cmd = cmd.Replace("DG", DDG.ToString());
            cmd = cmd.Replace("DB", DDB.ToString());
            cmd = cmd.Replace("ER", EER.ToString());
            cmd = cmd.Replace("EG", EEG.ToString());
            cmd = cmd.Replace("EB", EEB.ToString());
            cmd = cmd.Replace("FR", FFR.ToString());
            cmd = cmd.Replace("FG", FFG.ToString());
            cmd = cmd.Replace("FB", FFB.ToString());
            cmd = cmd.Replace("GR", GGR.ToString());
            cmd = cmd.Replace("GG", GGG.ToString());
            cmd = cmd.Replace("GB", GGB.ToString());
            cmd = cmd.Replace("HR", HHR.ToString());
            cmd = cmd.Replace("HG", HHG.ToString());
            cmd = cmd.Replace("HB", HHB.ToString());
            cmd = cmd.Replace(",", ", ");
            //led_wiz.Command = "PBA:"+cmd;
            //byte[] res3 = System.Text.Encoding.Unicode.GetBytes(cmd);
            // string str3 = System.Text.Encoding.Unicode.GetString(res3);
            //label4.Text = str3;
            //label2.Text = ledwiz.NumDevices.ToString();
            try
            {
                var tokens = cmd.Split(',');
                byte[] data = new byte[tokens.Length];
                for (int i = 0; i < tokens.Length; i++)
                {
                    data[i] = byte.Parse(tokens[i]);
                }

                int DVI = ((int)Pickwiz.Value - 1);
                ledwiz.PBA(DVI, data);
                ledwiz.SBA(DVI, 255, 255, 255, 255, 5);
                txtColors.BackColor = SystemColors.Window;
                outcodetxt.Text = "YAY IT'S WORKING";

            }
            catch
            {
                txtColors.BackColor = Color.LightPink;
                outcodetxt.Text = "Error in String, check for typo's";
            }
        }

        private void Wizer()
        {
            if (ledwiz.NumDevices > 0)
            {
                Pickwiz.Maximum = ledwiz.NumDevices;
                Pickwiz.Minimum = 1;
                Pickwiz.Value = 1;
            }
        }

        private void OpenSerial()
        {
            Wizer();
        }
        #endregion

        #region Controls

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void disableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EnableDisable();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void EnableDisable()
        {
            if (CaptureTimer.Enabled)
            {
//                DWM.DwmEnableComposition(true);
                this.Text = "Wizlighter DISABLED";
                EnableBtn.Text = "Enable";
                disableToolStripMenuItem.Text = "Enable";
                CaptureTimer.Enabled = false;
                for (int i = 0; i < ledwiz.NumDevices; i++)
                {
                    ledwiz.PBA(0, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                }
                Array.Clear(GlobalVariables.SendByte, 0, 9);
/*
                if (serialPort1.IsOpen)
                {
                    serialPort1.Write(GlobalVariables.SendByte, 0, 9); //send 9 zero chars to turn off the lights
                    serialPort1.Close();
                }
 */
                
            }
            else
            {
                //DWM.DwmEnableComposition(false);
                OpenSerial();
                Array.Clear(GlobalVariables.CurrentColor, 0, 9);
                EnableBtn.Text = "Disable";
                disableToolStripMenuItem.Text = "Disable";
                this.Text = "Wizlighter Enabled";
                CaptureTimer.Enabled = true;
            }
        }

        private void EnableBtn_Click(object sender, EventArgs e)
        {
            EnableDisable();
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }

        }

        private void ModeCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ModeCombo.Text == "Movie")
            {
                GlobalVariables.mode = 0;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                ModeCombo2.Text = "Movie";
                label31.Visible = false;
                fsr.Visible = false;
                SmoothDarkness.Visible = false;
                label38.Visible = false;
                AggressiveLevel.Visible = false;
                label46.Visible = false;
                ColorIncSmooth.Visible = false;
            }
            else if (ModeCombo.Text == "Gaming")
            {
                GlobalVariables.mode = 1;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                ModeCombo2.Text = "Gaming";
                label31.Visible = false;
                fsr.Visible = false;
                SmoothDarkness.Visible = false;
                label38.Visible = false;
                AggressiveLevel.Visible = false;
                label46.Visible = false;
                ColorIncSmooth.Visible = false;
            }
            else if (ModeCombo.Text == "10*Movie smooth")
            {
                GlobalVariables.mode = 2;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                ModeCombo2.Text = "10*Movie smooth";
                label31.Visible = false;
                fsr.Visible = false;
                SmoothDarkness.Visible = false;
                label38.Visible = false;
                AggressiveLevel.Visible = false;
                label46.Visible = false;
                ColorIncSmooth.Visible = false;
            }
            else if (ModeCombo.Text == "30*Movie smooth")
            {
                GlobalVariables.mode = 3;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                ModeCombo2.Text = "30*Movie smooth";
                label31.Visible = false;
                fsr.Visible = false;
                SmoothDarkness.Visible = false;
                label38.Visible = false;
                AggressiveLevel.Visible = false;
                label46.Visible = false;
                ColorIncSmooth.Visible = false;
            }
            else if (ModeCombo.Text == "10*Movie smooth+Fast scene")
            {
                GlobalVariables.mode = 4;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                ModeCombo2.Text = "10*Movie smooth+Fast scene";
                label31.Visible = true;
                fsr.Visible = true;
                SmoothDarkness.Visible = true;
                label38.Visible = true;
                AggressiveLevel.Visible = true;
                label46.Visible = true;
                ColorIncSmooth.Visible = true;
            }
        }

        private void ModeCombo2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ModeCombo2.Text == "Movie")
            {
                GlobalVariables.mode = 0;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                ModeCombo.Text = "Movie";
                label31.Visible = false;
                fsr.Visible = false;
                SmoothDarkness.Visible = false;
                label38.Visible = false;
                AggressiveLevel.Visible = false;
                label46.Visible = false;
                ColorIncSmooth.Visible = false;
            }
            else if (ModeCombo2.Text == "Gaming")
            {
                GlobalVariables.mode = 1;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;
                ModeCombo.Text = "Gaming";
                label31.Visible = false;
                fsr.Visible = false;
                SmoothDarkness.Visible = false;
                label38.Visible = false;
                AggressiveLevel.Visible = false;
                label46.Visible = false;
                ColorIncSmooth.Visible = false;
            }
            else if (ModeCombo2.Text == "10*Movie smooth")
            {
                GlobalVariables.mode = 2;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                ModeCombo.Text = "10*Movie smooth";
                label31.Visible = false;
                fsr.Visible = false;
                SmoothDarkness.Visible = false;
                label38.Visible = false;
                AggressiveLevel.Visible = false;
                label46.Visible = false;
                ColorIncSmooth.Visible = false;
            }
            else if (ModeCombo2.Text == "30*Movie smooth")
            {
                GlobalVariables.mode = 3;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                ModeCombo.Text = "30*Movie smooth";
                label31.Visible = false;
                fsr.Visible = false;
                SmoothDarkness.Visible = false;
                label38.Visible = false;
                AggressiveLevel.Visible = false;
                label46.Visible = false;
                ColorIncSmooth.Visible = false;
            }
            else if (ModeCombo2.Text == "10*Movie smooth+Fast scene")
            {
                GlobalVariables.mode = 4;
                System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal;
                ModeCombo.Text = "10*Movie smooth+Fast scene";
                label31.Visible = true;
                fsr.Visible = true;
                SmoothDarkness.Visible = true;
                label38.Visible = true;
                AggressiveLevel.Visible = true;
                label46.Visible = true;
                ColorIncSmooth.Visible = true;
            }
        }

        private void TimerDelay_ValueChanged(object sender, EventArgs e)
        {
            CaptureTimer.Interval = (int)TimerDelay.Value;
        }

        private void AutoDetect_Tick(object sender, EventArgs e)
        {
            bool foundit = false;
            Process[] prs = Process.GetProcesses();
            foreach (Process proces in prs)
            {
                if (proces.ProcessName == ProcessName.Text)
                {
                    foundit = true;
                }
            }

            if (foundit == true)
            {
                if (CaptureTimer.Enabled == false && AutoEnable.Checked)
                {
                    DWM.DwmEnableComposition(false);
                    EnableDisable();
                }

            }
            else
            {
                if (CaptureTimer.Enabled == true && AutoEnable.Checked)
                {
                    DWM.DwmEnableComposition(true);
                    EnableDisable();
                }
            }


        }

        private void AutoEnable_CheckedChanged(object sender, EventArgs e)
        {
            if (AutoEnable.Checked)
            {
                EnableBtn.Enabled = false;
            }
            else
            {
                EnableBtn.Enabled = true;
            }
        }

        #endregion

        #region Get Screen Shot

        public static Bitmap CreateThumbnail(Bitmap loBMP, int lnWidth, int lnHeight)
        {

            Bitmap bmpOut = null;

            try
            {

//                Bitmap loBMP = new Bitmap(lcFilename);
                ImageFormat loFormat = loBMP.RawFormat;

                decimal lnRatio;

                int lnNewWidth = 0;
                int lnNewHeight = 0;

                //*** If the image is smaller than a thumbnail just return it
/*
                if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
                    return loBMP;
 */
                if (loBMP.Width > loBMP.Height)
                {
                    lnRatio = (decimal)lnWidth / loBMP.Width;
                    lnNewWidth = lnWidth;
                    decimal lnTemp = loBMP.Height * lnRatio;
                    lnNewHeight = (int)lnTemp;
                }
                else
                {
                    lnRatio = (decimal)lnHeight / loBMP.Height;
                    lnNewHeight = lnHeight;
                    decimal lnTemp = loBMP.Width * lnRatio;
                    lnNewWidth = (int)lnTemp;
                }
                bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                Graphics g = Graphics.FromImage(bmpOut);
//                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
 //               g.InterpolationMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
 //               g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
                g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);

                loBMP.Dispose();
            }
            catch
            {
                return null;
            }
            return bmpOut;
        }


        private void DeskCapture()
        {
            int redB = 0, greenB = 0, blueB = 0;
            int redC = 0, greenC = 0, blueC = 0;
            int redA = 0, greenA = 0, blueA = 0;
            int redD = 0, greenD = 0, blueD = 0;
            int redE = 0, greenE = 0, blueE = 0;
            int redF = 0, greenF = 0, blueF = 0;
            int redG = 0, greenG = 0, blueG = 0;
            int redH = 0, greenH = 0, blueH = 0;
            int CounterB = 0, CounterC = 0, CounterA = 0, CounterD = 0;
            int CounterE = 0, CounterF = 0, CounterG = 0, CounterH = 0;
            bool read = false;
            bool CaptureScreen = false;
            
            Color c = Color.Black;

            Screen MyScreen = Screen.AllScreens[GlobalVariables.SelectedDisplay];

            if ((CaptureEvery.Checked == false) || (CaptureEvery.Checked == true && CyclesInt == 0))
            {
                CaptureScreen = true;
            }

            if (CaptureEvery.Checked == true)
            {
                CyclesInt = CyclesInt + 1;
            }

            if (CaptureScreen == true)
            {
                b = new Bitmap(MyScreen.Bounds.Width, MyScreen.Bounds.Height);
                g = Graphics.FromImage(b);
                g.CopyFromScreen(MyScreen.Bounds.Left, MyScreen.Bounds.Top, 0, 0, new Size(MyScreen.Bounds.Width, MyScreen.Bounds.Height));
            }

            // b.Save("temp.jpg", ImageFormat.Jpeg);

            swidth = b.Width;
            sheight = b.Height;

            // b.Save("temp.jpg", ImageFormat.Jpeg);

            int divide = (int)DivideLevel.Value;
            GlobalVariables.Offset = (int)OffsetLevel.Value;
            if (GlobalVariables.mode == 1)
            {
                divide = 32;
                GlobalVariables.Offset = 0;
            }

            int dx = swidth / divide;
            int dy = sheight / divide;

            int offsetx = dx / 3 * GlobalVariables.Offset;
            int offsety = dy / 3 * GlobalVariables.Offset;

            if (this.WindowState != FormWindowState.Minimized)
            {
                label36.Text = MyScreen.Bounds.Width + "x" + MyScreen.Bounds.Height + " (" + MyScreen.Bounds.Left + ", " + MyScreen.Bounds.Top + ")";

                label60.Text = dx.ToString();
                label62.Text = dy.ToString();

                label66.Text = swidth.ToString();
                label69.Text = sheight.ToString();

                label64.Text = "~" + divide.ToString();
                label65.Text = "~" + divide.ToString();

                label74.Text = offsetx.ToString();
                label72.Text = offsety.ToString();
            }

            for (int PixelY = offsety; PixelY < sheight; PixelY += dy)
                {
                for (int PixelX = offsetx; PixelX < swidth; PixelX += swidth / divide)
                {

                    {
                        read = false;

                        if (PixelX >= swidth * BBHS.Value / 100 && PixelX <= swidth * BBHE.Value / 100  && PixelY >= sheight * BBVS.Value /100 && PixelY <= sheight * BBVE.Value /100)
                        {
                            if (read == false)
                            {
                                c = b.GetPixel(PixelX, PixelY);
                                read = true;
                            }
                            redB += c.R;
                            greenB += c.G;
                            blueB += c.B;
                            CounterB++;
                        }
                        if (PixelX >= swidth * CCHS.Value / 100 && PixelX <= swidth * CCHE.Value / 100 && PixelY >= sheight * CCVS.Value / 100 && PixelY <= sheight * CCVE.Value / 100)
                        {
                            if (read == false)
                            {
                                c = b.GetPixel(PixelX, PixelY);
                                read = true;
                            }
                            redC += c.R;
                            greenC += c.G;
                            blueC += c.B;
                            CounterC++;
                        }
                        if (PixelX >= swidth * AAHS.Value / 100 && PixelX <= swidth * AAHE.Value / 100 && PixelY >= sheight * AAVS.Value / 100 && PixelY <= sheight * AAVE.Value / 100)
                        {
                            if (read == false)
                            {
                                c = b.GetPixel(PixelX, PixelY);
                                read = true;
                            }
                            redA += c.R;
                            greenA += c.G;
                            blueA += c.B;
                            CounterA++;
                        }
                        if (PixelX >= swidth * DDHS.Value / 100 && PixelX <= swidth * DDHE.Value / 100 && PixelY >= sheight * DDVS.Value / 100 && PixelY <= sheight * DDVE.Value / 100)
                        {
                            if (read == false)
                            {
                                c = b.GetPixel(PixelX, PixelY);
                                read = true;
                            }
                            redD += c.R;
                            greenD += c.G;
                            blueD += c.B;
                            CounterD++;
                        }
                        if (PixelX >= swidth * EEHS.Value / 100 && PixelX <= swidth * EEHE.Value / 100 && PixelY >= sheight * EEVS.Value / 100 && PixelY <= sheight * EEVE.Value / 100)
                        {
                            if (read == false)
                            {
                                c = b.GetPixel(PixelX, PixelY);
                                read = true;
                            }
                            redE += c.R;
                            greenE += c.G;
                            blueE += c.B;
                            CounterE++;
                        }
                        if (PixelX >= swidth * FFHS.Value / 100 && PixelX <= swidth * FFHE.Value / 100 && PixelY >= sheight * FFVS.Value / 100 && PixelY <= sheight * FFVE.Value / 100)
                        {
                            if (read == false)
                            {
                                c = b.GetPixel(PixelX, PixelY);
                                read = true;
                            }
                            redF += c.R;
                            greenF += c.G;
                            blueF += c.B;
                            CounterF++;
                        }
                        if (PixelX >= swidth * GGHS.Value / 100 && PixelX <= swidth * GGHE.Value / 100 && PixelY >= sheight * GGVS.Value / 100 && PixelY <= sheight * GGVE.Value / 100)
                        {
                            if (read == false)
                            {
                                c = b.GetPixel(PixelX, PixelY);
                                read = true;
                            }
                            redG += c.R;
                            greenG += c.G;
                            blueG += c.B;
                            CounterG++;
                        }
                        if (PixelX >= swidth * HHHS.Value / 100 && PixelX <= swidth * HHHE.Value / 100 && PixelY >= sheight * HHVS.Value / 100 && PixelY <= sheight * HHVE.Value / 100)
                        {
                            if (read == false)
                            {
                                c = b.GetPixel(PixelX, PixelY);
                                read = true;
                            }
                            redH += c.R;
                            greenH += c.G;
                            blueH += c.B;
                            CounterH++;
                        }
                    }
                }
            }

            if (CounterB > 0)
            {
                redB /= CounterB;
                greenB /= CounterB;
                blueB /= CounterB;
            }
            if (CounterA > 0)
            {
                redA /= CounterA;
                greenA /= CounterA;
                blueA /= CounterA;
            }
            if (CounterC > 0)
            {
                redC /= CounterC;
                greenC /= CounterC;
                blueC /= CounterC;
            }
            if (CounterD > 0)
            {
                redD /= CounterD;
                greenD /= CounterD;
                blueD /= CounterD;
            }
            if (CounterE > 0)
            {
                redE /= CounterE;
                greenE /= CounterE;
                blueE /= CounterE;
            }
            if (CounterF > 0)
            {
                redF /= CounterF;
                greenF /= CounterF;
                blueF /= CounterF;
            }
            if (CounterG > 0)
            {
                redG /= CounterG;
                greenG /= CounterG;
                blueG /= CounterG;
            }
            if (CounterH > 0)
            {
                redH /= CounterH;
                greenH /= CounterH;
                blueH /= CounterH;
            }
            if (redB > 0 || greenB > 0 || blueB > 0 || redA > 0 || greenA > 0 || blueA > 0 || redC > 0 || greenC > 0 || blueC > 0 || redD > 0 || greenD > 0 || blueD > 0 || redE > 0 || greenE > 0 || blueE > 0 || redF > 0 || greenF > 0 || blueF > 0 || redG > 0 || greenG > 0 || blueG > 0 || redH > 0 || greenH > 0 || blueH > 0 || GlobalVariables.blackframe)
            {
                GlobalVariables.blackframe = false;
                Array.Copy(GlobalVariables.PreviousColor29, GlobalVariables.PreviousColor30, 32);
                Array.Copy(GlobalVariables.PreviousColor28, GlobalVariables.PreviousColor29, 32);
                Array.Copy(GlobalVariables.PreviousColor27, GlobalVariables.PreviousColor28, 32);
                Array.Copy(GlobalVariables.PreviousColor26, GlobalVariables.PreviousColor27, 32);
                Array.Copy(GlobalVariables.PreviousColor25, GlobalVariables.PreviousColor26, 32);
                Array.Copy(GlobalVariables.PreviousColor24, GlobalVariables.PreviousColor25, 32);
                Array.Copy(GlobalVariables.PreviousColor23, GlobalVariables.PreviousColor24, 32);
                Array.Copy(GlobalVariables.PreviousColor22, GlobalVariables.PreviousColor23, 32);
                Array.Copy(GlobalVariables.PreviousColor21, GlobalVariables.PreviousColor22, 32);
                Array.Copy(GlobalVariables.PreviousColor20, GlobalVariables.PreviousColor21, 32);
                Array.Copy(GlobalVariables.PreviousColor19, GlobalVariables.PreviousColor20, 32);
                Array.Copy(GlobalVariables.PreviousColor18, GlobalVariables.PreviousColor19, 32);
                Array.Copy(GlobalVariables.PreviousColor17, GlobalVariables.PreviousColor18, 32);
                Array.Copy(GlobalVariables.PreviousColor16, GlobalVariables.PreviousColor17, 32);
                Array.Copy(GlobalVariables.PreviousColor15, GlobalVariables.PreviousColor16, 32);
                Array.Copy(GlobalVariables.PreviousColor14, GlobalVariables.PreviousColor15, 32);
                Array.Copy(GlobalVariables.PreviousColor13, GlobalVariables.PreviousColor14, 32);
                Array.Copy(GlobalVariables.PreviousColor12, GlobalVariables.PreviousColor13, 32);
                Array.Copy(GlobalVariables.PreviousColor11, GlobalVariables.PreviousColor12, 32);
                Array.Copy(GlobalVariables.PreviousColor10, GlobalVariables.PreviousColor11, 32);
                Array.Copy(GlobalVariables.PreviousColor9, GlobalVariables.PreviousColor10, 32);
                Array.Copy(GlobalVariables.PreviousColor8, GlobalVariables.PreviousColor9, 32);
                Array.Copy(GlobalVariables.PreviousColor7, GlobalVariables.PreviousColor8, 32);
                Array.Copy(GlobalVariables.PreviousColor6, GlobalVariables.PreviousColor7, 32);
                Array.Copy(GlobalVariables.PreviousColor5, GlobalVariables.PreviousColor6, 32);
                Array.Copy(GlobalVariables.PreviousColor4, GlobalVariables.PreviousColor5, 32);
                Array.Copy(GlobalVariables.PreviousColor3, GlobalVariables.PreviousColor4, 32);
                Array.Copy(GlobalVariables.PreviousColor2, GlobalVariables.PreviousColor3, 32);
                Array.Copy(GlobalVariables.PreviousColor, GlobalVariables.PreviousColor2, 32);
                Array.Copy(GlobalVariables.CurrentColor, GlobalVariables.PreviousColor, 32);

                GlobalVariables.CurrentColor[0] = redA;
                GlobalVariables.CurrentColor[1] = redB;
                GlobalVariables.CurrentColor[2] = redC;
                GlobalVariables.CurrentColor[3] = greenA;
                GlobalVariables.CurrentColor[4] = greenB;
                GlobalVariables.CurrentColor[5] = greenC;
                GlobalVariables.CurrentColor[6] = blueA;
                GlobalVariables.CurrentColor[7] = blueB;
                GlobalVariables.CurrentColor[8] = blueC;
                GlobalVariables.CurrentColor[9] = redD;
                GlobalVariables.CurrentColor[10] = redE;
                GlobalVariables.CurrentColor[11] = redF;
                GlobalVariables.CurrentColor[12] = greenD;
                GlobalVariables.CurrentColor[13] = greenE;
                GlobalVariables.CurrentColor[14] = greenF;
                GlobalVariables.CurrentColor[15] = blueD;
                GlobalVariables.CurrentColor[16] = blueE;
                GlobalVariables.CurrentColor[17] = blueF;
                GlobalVariables.CurrentColor[18] = redG;
                GlobalVariables.CurrentColor[19] = redH;
                //GlobalVariables.CurrentColor[20] = redI;
                GlobalVariables.CurrentColor[21] = greenG;
                GlobalVariables.CurrentColor[22] = greenH;
                //GlobalVariables.CurrentColor[23] = greenI;
                GlobalVariables.CurrentColor[24] = blueG;
                GlobalVariables.CurrentColor[25] = blueH;
               // GlobalVariables.CurrentColor[26] = blueI;

                //if (checkBox1.Checked)
                if (radioButton2.Checked)
                {
                    int[] Highest = new int[3] { 0, 0, 0 };
                    for (int I = 0; I < 25; I += 3)
                    {
                        if (Highest[0] < GlobalVariables.CurrentColor[I]) Highest[0] = GlobalVariables.CurrentColor[I];
                        if (Highest[1] < GlobalVariables.CurrentColor[I + 1]) Highest[1] = GlobalVariables.CurrentColor[I + 1];
                        if (Highest[2] < GlobalVariables.CurrentColor[I + 2]) Highest[2] = GlobalVariables.CurrentColor[I + 2];
                    }
                    float[] Multiply = new float[3];
                    for (int I = 0; I < 3; I++)
                    {
                        Multiply[I] = 255.0F / (float)Highest[I];
                        if (Multiply[I] > (float)numericUpDown1.Value) Multiply[I] = (float)numericUpDown1.Value;
                    }
                    for (int I = 0; I < 25; I += 3)
                    {
                        GlobalVariables.CurrentColor[I] = (int)((float)GlobalVariables.CurrentColor[I] * Multiply[0]);
                        GlobalVariables.CurrentColor[I + 1] = (int)((float)GlobalVariables.CurrentColor[I + 1] * Multiply[1]);
                        GlobalVariables.CurrentColor[I + 2] = (int)((float)GlobalVariables.CurrentColor[I + 2] * Multiply[2]);
                    }
                }
                
                
                
                if (radioButton3.Checked)
                {
                    int Highest = 0;
                    for (int I = 0; I < 64; I++)
                    {
                        if (Highest < GlobalVariables.CurrentColor[I])
                            Highest = GlobalVariables.CurrentColor[I];
                    }
                    float Multiply = 255.0F / (float)Highest;
                    if (Multiply > (float)numericUpDown1.Value)
                    {
                        Multiply = (float)numericUpDown1.Value;
                    }
                    for (int I = 0; I < 64; I++)
                    {
                        GlobalVariables.CurrentColor[I] = (int)((float)GlobalVariables.CurrentColor[I] * Multiply);
                    }
                }
                GlobalVariables.Offset++;
                if (GlobalVariables.Offset == 3)
                {
                    GlobalVariables.Offset = 0;
                }
            }
            else
            {
                GlobalVariables.blackframe = true;
            }

            if (CaptureScreen == true)
            {
            }

            if ((CyclesInt > CaptureCycles.Value) || (CaptureEvery.Checked == false))
            {
                CyclesInt = 0;
                g.Dispose();
                b.Dispose();
            }

        }



        private void EnumScreens()
        {
            Screen[] screens = Screen.AllScreens;
            int upperBound = screens.GetUpperBound(0);
            GlobalVariables.NrDisplays = upperBound;

            for (int I = 0; I <= upperBound; I++)
            {
                DisplaysBox.Items.Add("Display " + I.ToString());
            }
        }

        private void DisplaysBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            GlobalVariables.SelectedDisplay = Convert.ToInt32(DisplaysBox.Text.Remove(0, 8), 10);
        }

        #endregion

        #region Settions

        public void SAVEsettingS()
        {
            Properties.Settings.Default.AAHE = (int)AAHE.Value;
            Properties.Settings.Default.AAHS = (int)AAHS.Value;
            Properties.Settings.Default.AAVE = (int)AAVE.Value;
            Properties.Settings.Default.AAVS = (int)AAVS.Value;
            Properties.Settings.Default.BBHE = (int)BBHE.Value;
            Properties.Settings.Default.BBHS = (int)BBHS.Value;
            Properties.Settings.Default.BBVE = (int)BBVE.Value;
            Properties.Settings.Default.BBVS = (int)BBVS.Value;
            Properties.Settings.Default.CCHE = (int)CCHE.Value;
            Properties.Settings.Default.CCHS = (int)CCHS.Value;
            Properties.Settings.Default.CCVE = (int)CCVE.Value;
            Properties.Settings.Default.CCVS = (int)CCVS.Value;
            Properties.Settings.Default.DDHE = (int)DDHE.Value;
            Properties.Settings.Default.DDHS = (int)DDHS.Value;
            Properties.Settings.Default.DDVE = (int)DDVE.Value;
            Properties.Settings.Default.DDVS = (int)AAVS.Value;
            Properties.Settings.Default.EEHE = (int)EEHE.Value;
            Properties.Settings.Default.EEHS = (int)EEHS.Value;
            Properties.Settings.Default.EEVE = (int)EEVE.Value;
            Properties.Settings.Default.EEVS = (int)EEVS.Value;
            Properties.Settings.Default.FFHE = (int)FFHE.Value;
            Properties.Settings.Default.FFHS = (int)FFHS.Value;
            Properties.Settings.Default.FFVE = (int)FFVE.Value;
            Properties.Settings.Default.FFVS = (int)FFVS.Value;
            Properties.Settings.Default.GGHE = (int)GGHE.Value;
            Properties.Settings.Default.GGHS = (int)GGHS.Value;
            Properties.Settings.Default.GGVE = (int)GGVE.Value;
            Properties.Settings.Default.GGVS = (int)GGVS.Value;
            Properties.Settings.Default.HHHE = (int)HHHE.Value;
            Properties.Settings.Default.HHHS = (int)HHHS.Value;
            Properties.Settings.Default.HHVE = (int)HHVE.Value;
            Properties.Settings.Default.HHVS = (int)HHVS.Value;
            Properties.Settings.Default.AggressiveLevel = AggressiveLevel.Value;
            Properties.Settings.Default.AutoEnable = AutoEnable.Checked;
            Properties.Settings.Default.CaptureEvery = CaptureEvery.Checked;
            Properties.Settings.Default.ColorIncSmooth = ColorIncSmooth.Value;
            Properties.Settings.Default.Colorslider = Colorslider.Value;
            Properties.Settings.Default.fsr = fsr.Value;
            Properties.Settings.Default.numericUpDown1 = numericUpDown1.Value;
            Properties.Settings.Default.ProcessName = ProcessName.Text;
            Properties.Settings.Default.txtColors = txtColors.Text;
            Properties.Settings.Default.TimerDelay = TimerDelay.Value;
            Properties.Settings.Default.MODE = GlobalVariables.mode;
            Properties.Settings.Default.SmoothDarkness = SmoothDarkness.Checked;
            Properties.Settings.Default.Lightpick1 = Lightpick1.Text;
            Properties.Settings.Default.Lightpick2 = Lightpick2.Text;
            Properties.Settings.Default.Lightpick3 = Lightpick3.Text;
            Properties.Settings.Default.Lightpick4 = Lightpick4.Text;
            Properties.Settings.Default.Lightpick5 = Lightpick5.Text;
            Properties.Settings.Default.Lightpick6 = Lightpick6.Text;
            Properties.Settings.Default.Lightpick7 = Lightpick7.Text;
            Properties.Settings.Default.Lightpick8 = Lightpick8.Text;
            Properties.Settings.Default.Lightpick9 = Lightpick9.Text;
            Properties.Settings.Default.Lightpick10 = Lightpick10.Text;
            Properties.Settings.Default.CaptureCycles = CaptureCycles.Value;
            Properties.Settings.Default.offsetlvl = OffsetLevel.Value;
            Properties.Settings.Default.modecombo2 = ModeCombo2.Text;
            Properties.Settings.Default.modecombo = ModeCombo.Text;
            Properties.Settings.Default.Save();
        }

        public void GETsettingS()
        {
            AAHE.Value = Properties.Settings.Default.AAHE;
            AAHS.Value = Properties.Settings.Default.AAHS;
            AAVE.Value = Properties.Settings.Default.AAVE;
            AAVS.Value = Properties.Settings.Default.AAVS;
            BBHE.Value = Properties.Settings.Default.BBHE;
            BBHS.Value = Properties.Settings.Default.BBHS;
            BBVE.Value = Properties.Settings.Default.BBVE;
            BBVS.Value = Properties.Settings.Default.BBVS;
            CCHE.Value = Properties.Settings.Default.CCHE;
            CCHS.Value = Properties.Settings.Default.CCHS;
            CCVE.Value = Properties.Settings.Default.CCVE;
            CCVS.Value = Properties.Settings.Default.CCVS;
            DDHE.Value = Properties.Settings.Default.DDHE;
            DDHS.Value = Properties.Settings.Default.DDHS;
            DDVE.Value = Properties.Settings.Default.DDVE;
            DDVS.Value = Properties.Settings.Default.DDVS;
            EEHE.Value = Properties.Settings.Default.EEHE;
            EEHS.Value = Properties.Settings.Default.EEHS;
            EEVE.Value = Properties.Settings.Default.EEVE;
            EEVS.Value = Properties.Settings.Default.EEVS;
            FFHE.Value = Properties.Settings.Default.FFHE;
            FFHS.Value = Properties.Settings.Default.FFHS;
            FFVE.Value = Properties.Settings.Default.FFVE;
            FFVS.Value = Properties.Settings.Default.FFVS;
            GGHE.Value = Properties.Settings.Default.GGHE;
            GGHS.Value = Properties.Settings.Default.GGHS;
            GGVE.Value = Properties.Settings.Default.GGVE;
            GGVS.Value = Properties.Settings.Default.GGVS;
            HHHE.Value = Properties.Settings.Default.HHHE;
            HHHS.Value = Properties.Settings.Default.HHHS;
            HHVE.Value = Properties.Settings.Default.HHVE;
            HHVS.Value = Properties.Settings.Default.HHVS;
            AggressiveLevel.Value = Properties.Settings.Default.AggressiveLevel;
            AutoEnable.Checked = Properties.Settings.Default.AutoEnable;
            CaptureEvery.Checked = Properties.Settings.Default.CaptureEvery;
            ColorIncSmooth.Value = Properties.Settings.Default.ColorIncSmooth;
            Colorslider.Value = Properties.Settings.Default.Colorslider;
            fsr.Value = Properties.Settings.Default.fsr;
            numericUpDown1.Value = Properties.Settings.Default.numericUpDown1;
            ProcessName.Text = Properties.Settings.Default.ProcessName;
            txtColors.Text = Properties.Settings.Default.txtColors;
            TimerDelay.Value = Properties.Settings.Default.TimerDelay;
            GlobalVariables.mode = Properties.Settings.Default.MODE;
            CaptureCycles.Value = Properties.Settings.Default.CaptureCycles;
            SmoothDarkness.Checked = Properties.Settings.Default.SmoothDarkness;
            Lightpick1.Text = Properties.Settings.Default.Lightpick1;
            Lightpick2.Text = Properties.Settings.Default.Lightpick2;
            Lightpick3.Text = Properties.Settings.Default.Lightpick3;
            Lightpick4.Text = Properties.Settings.Default.Lightpick4;
            Lightpick5.Text = Properties.Settings.Default.Lightpick5;
            Lightpick6.Text = Properties.Settings.Default.Lightpick6;
            Lightpick7.Text = Properties.Settings.Default.Lightpick7;
            Lightpick8.Text = Properties.Settings.Default.Lightpick8;
            Lightpick9.Text = Properties.Settings.Default.Lightpick9;
            Lightpick10.Text = Properties.Settings.Default.Lightpick10;
            ModeCombo.Text = Properties.Settings.Default.modecombo;
            ModeCombo2.Text = Properties.Settings.Default.modecombo2;
            OffsetLevel.Value = Properties.Settings.Default.offsetlvl;
        }

        #endregion

        #region Builder 
        //Crude but works well
        string MakerBase = "light1,light2,light3,light4,light5,light6,light7,light8,light9,lightT,0,0";

        private void Maker()
        {
            #region Light 1
            if (Lightpick1.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("light1", "AR,AG,AB");
            }
            else if (Lightpick1.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("light1", "BR,BG,BB");
            }
            else if (Lightpick1.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("light1", "CR,CG,CB");
            }
            else if (Lightpick1.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("light1", "DR,DG,DB");
            }
            else if (Lightpick1.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("light1", "ER,EG,EB");
            }
            else if (Lightpick1.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("light1", "FR,FG,FB");
            }
            else if (Lightpick1.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("light1", "GR,GG,GB");
            }
            else if (Lightpick1.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("light1", "HR,HG,HB");
            }
            else if (Lightpick1.Text == "Off")
            {
                MakerBase = MakerBase.Replace("light1", "0,0,0");
            }
            else
            {
                MakerBase = MakerBase.Replace("light1", "0,0,0");
            }


            #endregion

            #region Light 2



            if (Lightpick2.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("light2", "AR,AG,AB");
            }
            else if (Lightpick2.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("light2", "BR,BG,BB");
            }
            else if (Lightpick2.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("light2", "CR,CG,CB");
            }
            else if (Lightpick2.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("light2", "DR,DG,DB");
            }
            else if (Lightpick2.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("light2", "ER,EG,EB");
            }
            else if (Lightpick2.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("light2", "FR,FG,FB");
            }
            else if (Lightpick2.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("light2", "GR,GG,GB");
            }
            else if (Lightpick2.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("light2", "HR,HG,HB");
            }
            else if (Lightpick2.Text == "Off")
            {
                MakerBase = MakerBase.Replace("light2", "0,0,0");
            }
            else
            {
                MakerBase = MakerBase.Replace("light2", "0,0,0");
            }

            #endregion

            #region Light 3


            if (Lightpick3.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("light3", "AR,AG,AB");
            }
            else if (Lightpick3.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("light3", "BR,BG,BB");
            }
            else if (Lightpick3.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("light3", "CR,CG,CB");
            }
            else if (Lightpick3.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("light3", "DR,DG,DB");
            }
            else if (Lightpick3.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("light3", "ER,EG,EB");
            }
            else if (Lightpick3.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("light3", "FR,FG,FB");
            }
            else if (Lightpick3.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("light3", "GR,GG,GB");
            }
            else if (Lightpick3.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("light3", "HR,HG,HB");
            }
            else if (Lightpick3.Text == "Off")
            {
                MakerBase = MakerBase.Replace("light3", "0,0,0");
            }
            else
            {
                MakerBase = MakerBase.Replace("light3", "0,0,0");
            }


            #endregion

            #region Light 4

            if (Lightpick4.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("light4", "AR,AG,AB");
            }
            else if (Lightpick4.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("light4", "BR,BG,BB");
            }
            else if (Lightpick4.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("light4", "CR,CG,CB");
            }
            else if (Lightpick4.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("light4", "DR,DG,DB");
            }
            else if (Lightpick4.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("light4", "ER,EG,EB");
            }
            else if (Lightpick4.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("light4", "FR,FG,FB");
            }
            else if (Lightpick4.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("light4", "GR,GG,GB");
            }
            else if (Lightpick4.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("light4", "HR,HG,HB");
            }
            else if (Lightpick4.Text == "Off")
            {
                MakerBase = MakerBase.Replace("light4", "0,0,0");
            }
            else
            {
                MakerBase = MakerBase.Replace("light4", "0,0,0");
            }


            #endregion

            #region Light 5

            if (Lightpick5.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("light5", "AR,AG,AB");
            }
            else if (Lightpick5.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("light5", "BR,BG,BB");
            }
            else if (Lightpick5.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("light5", "CR,CG,CB");
            }
            else if (Lightpick5.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("light5", "DR,DG,DB");
            }
            else if (Lightpick5.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("light5", "ER,EG,EB");
            }
            else if (Lightpick5.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("light5", "FR,FG,FB");
            }
            else if (Lightpick5.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("light5", "GR,GG,GB");
            }
            else if (Lightpick5.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("light5", "HR,HG,HB");
            }
            else if (Lightpick5.Text == "Off")
            {
                MakerBase = MakerBase.Replace("light5", "0,0,0");
            }
            else
            {
                MakerBase = MakerBase.Replace("light5", "0,0,0");
            }

            #endregion

            #region light 6

            if (Lightpick6.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("light6", "AR,AG,AB");
            }
            else if (Lightpick6.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("light6", "BR,BG,BB");
            }
            else if (Lightpick6.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("light6", "CR,CG,CB");
            }
            else if (Lightpick6.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("light6", "DR,DG,DB");
            }
            else if (Lightpick6.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("light6", "ER,EG,EB");
            }
            else if (Lightpick6.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("light6", "FR,FG,FB");
            }
            else if (Lightpick6.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("light6", "GR,GG,GB");
            }
            else if (Lightpick6.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("light6", "HR,HG,HB");
            }
            else if (Lightpick6.Text == "Off")
            {
                MakerBase = MakerBase.Replace("light6", "0,0,0");
            }
            else
            {
                MakerBase = MakerBase.Replace("light6", "0,0,0");
            }

            #endregion

            #region Light 7


            if (Lightpick7.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("light7", "AR,AG,AB");
            }
            else if (Lightpick7.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("light7", "BR,BG,BB");
            }
            else if (Lightpick7.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("light7", "CR,CG,CB");
            }
            else if (Lightpick7.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("light7", "DR,DG,DB");
            }
            else if (Lightpick7.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("light7", "ER,EG,EB");
            }
            else if (Lightpick7.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("light7", "FR,FG,FB");
            }
            else if (Lightpick7.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("light7", "GR,GG,GB");
            }
            else if (Lightpick7.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("light7", "HR,HG,HB");
            }
            else if (Lightpick7.Text == "Off")
            {
                MakerBase = MakerBase.Replace("light7", "0,0,0");
            }
            else
            {
                MakerBase = MakerBase.Replace("light7", "0,0,0");
            }
            #endregion

            #region Light 8

            if (Lightpick8.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("light8", "AR,AG,AB");
            }
            else if (Lightpick8.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("light8", "BR,BG,BB");
            }
            else if (Lightpick8.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("light8", "CR,CG,CB");
            }
            else if (Lightpick8.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("light8", "DR,DG,DB");
            }
            else if (Lightpick8.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("light8", "ER,EG,EB");
            }
            else if (Lightpick8.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("light8", "FR,FG,FB");
            }
            else if (Lightpick8.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("light8", "GR,GG,GB");
            }
            else if (Lightpick8.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("light8", "HR,HG,HB");
            }
            else if (Lightpick8.Text == "Off")
            {
                MakerBase = MakerBase.Replace("light8", "0,0,0");
            }
            else
            {
                MakerBase = MakerBase.Replace("light8", "0,0,0");
            }

            #endregion

            #region light 9

            if (Lightpick9.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("light9", "AR,AG,AB");
            }
            else if (Lightpick9.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("light9", "BR,BG,BB");
            }
            else if (Lightpick9.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("light9", "CR,CG,CB");
            }
            else if (Lightpick9.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("light9", "DR,DG,DB");
            }
            else if (Lightpick9.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("light9", "ER,EG,EB");
            }
            else if (Lightpick9.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("light9", "FR,FG,FB");
            }
            else if (Lightpick9.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("light9", "GR,GG,GB");
            }
            else if (Lightpick9.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("light9", "HR,HG,HB");
            }
            else if (Lightpick9.Text == "Off")
            {
                MakerBase = MakerBase.Replace("light9", "0,0,0");
            }
            else
            {
               MakerBase = MakerBase.Replace("light9", "0,0,0");
            }

            #endregion

            #region light 10

            if (Lightpick10.Text == "Light A")
            {
                MakerBase = MakerBase.Replace("lightT", "AR,AG,AB");
            }
            else if (Lightpick10.Text == "Light B")
            {
                MakerBase = MakerBase.Replace("lightT", "BR,BG,BB");
            }
            else if (Lightpick10.Text == "Light C")
            {
                MakerBase = MakerBase.Replace("lightT", "CR,CG,CB");
            }
            else if (Lightpick10.Text == "Light D")
            {
                MakerBase = MakerBase.Replace("lightT", "DR,DG,DB");
            }
            else if (Lightpick10.Text == "Light E")
            {
                MakerBase = MakerBase.Replace("lightT", "ER,EG,EB");
            }
            else if (Lightpick10.Text == "Light F")
            {
                MakerBase = MakerBase.Replace("lightT", "FR,FG,FB");
            }
            else if (Lightpick10.Text == "Light G")
            {
                MakerBase = MakerBase.Replace("lightT", "GR,GG,GB");
            }
            else if (Lightpick10.Text == "Light H")
            {
                MakerBase = MakerBase.Replace("lightT", "HR,HG,HB");
            }
            else if (Lightpick10.Text == "Off")
            {
                MakerBase = MakerBase.Replace("lightT", "0,0,0");
            }
            else
            {
                MakerBase = MakerBase.Replace("lightT", "0,0,0");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Maker();
            makertest.Text = MakerBase;
            txtColors.Text = MakerBase;
            MakerBase = "light1,light2,light3,light4,light5,light6,light7,light8,light9,lightT,0,0";
        }
            #endregion

        #endregion
    }
}